package cn.sqlog.simplewol.base;




import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.util.NestedServletException;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@ResponseBody
public class ExceptionController {
    //捕捉文件上传异常

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResponseBean uploadexception(MaxUploadSizeExceededException e) {
        return new ResponseBean(401, e.getMessage(), null);
    }

//    // 捕捉shiro的异常
//    @ResponseStatus(HttpStatus.UNAUTHORIZED)
//    @ExceptionHandler(ShiroException.class)
//    public ResponseBean handle401(ShiroException e) {
//        return new ResponseBean(401, e.getMessage(), "请检查是否拥有该权限");
//    }
//
//    // 捕捉UnauthorizedException
//    @ResponseStatus(HttpStatus.UNAUTHORIZED)
//    @ExceptionHandler(UnauthorizedException.class)
//    public ResponseBean handle401() {
//        return new ResponseBean(401, "Unauthorized", null);
//    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseBean handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        return new ResponseBean(100, "Error: parameter (Is it json?)", e.getMessage());
    }
//    // 捕捉微信用户为空异常
//    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
//    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
//    public ResponseBean sQLIntegrityConstraintViolationException(HttpServletRequest request, Throwable ex) {
//        return new ResponseBean(getStatus(request).value(), "请绑定微信用户在进行答题", "请绑定微信用户在进行答题");
//    }
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(NestedServletException.class)
    public ResponseBean handleHttpMessageNotReadableException(NestedServletException e) {
        return new ResponseBean(100, "Error: parameter (Is it json?)", e.getMessage());
    }



    /**
     * 405 - Method Not Allowed
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseBean handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return new ResponseBean(405, "Error: 405", e.getMessage());
    }

    /**
     * 404 - Not Found
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    public ResponseBean noHandlerFoundException(NoHandlerFoundException e) {
        return new ResponseBean(404, "Error: 404", e.getMessage());

    }
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseBean noIllegalArgumentException(IllegalArgumentException e) {
        return new ResponseBean(404, "错误的mac地址", e.getMessage());

    }




//    // 捕捉其他所有异常
//    @ExceptionHandler(Exception.class)
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    public ResponseBean globalException(HttpServletRequest request, Throwable ex) {
//        return new ResponseBean(getStatus(request).value(), ex.getMessage(), null,request.getParameter("refresh_token"),request.getParameter("openid"));
//    }

    private HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return HttpStatus.valueOf(statusCode);
    }
}
